home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / tar-1_11.lha / tar-1.11.2 / getopt1.c < prev    next >
C/C++ Source or Header  |  1993-01-17  |  3KB  |  162 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify it
  5.    under the terms of the GNU General Public License as published by the
  6.    Free Software Foundation; either version 2, or (at your option) any
  7.    later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #include "getopt.h"
  23.  
  24. #if !__STDC__ && !defined(const) && IN_GCC
  25. #define const
  26. #endif
  27.  
  28. #include <stdio.h>
  29.  
  30. /* This needs to come after some library #include
  31.    to get __GNU_LIBRARY__ defined.  */
  32. #ifdef __GNU_LIBRARY__
  33. #include <stdlib.h>
  34. #else
  35. char *getenv ();
  36. #endif
  37.  
  38. #ifndef    NULL
  39. #define NULL 0
  40. #endif
  41.  
  42. int
  43. getopt_long (argc, argv, options, long_options, opt_index)
  44.      int argc;
  45.      char *const *argv;
  46.      const char *options;
  47.      const struct option *long_options;
  48.      int *opt_index;
  49. {
  50.   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  51. }
  52.  
  53. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  54.    If an option that starts with '-' (not '--') doesn't match a long option,
  55.    but does match a short option, it is parsed as a short option
  56.    instead.  */
  57.  
  58. int 
  59. getopt_long_only (argc, argv, options, long_options, opt_index)
  60.      int argc;
  61.      char *const *argv;
  62.      const char *options;
  63.      const struct option *long_options;
  64.      int *opt_index;
  65. {
  66.   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  67. }
  68.  
  69. #ifdef TEST
  70.  
  71. #include <stdio.h>
  72.  
  73. int
  74. main (argc, argv)
  75.      int argc;
  76.      char **argv;
  77. {
  78.   int c;
  79.   int digit_optind = 0;
  80.  
  81.   while (1)
  82.     {
  83.       int this_option_optind = optind ? optind : 1;
  84.       int option_index = 0;
  85.       static struct option long_options[] =
  86.       {
  87.     {"add", 1, 0, 0},
  88.     {"append", 0, 0, 0},
  89.     {"delete", 1, 0, 0},
  90.     {"verbose", 0, 0, 0},
  91.     {"create", 0, 0, 0},
  92.     {"file", 1, 0, 0},
  93.     {0, 0, 0, 0}
  94.       };
  95.  
  96.       c = getopt_long (argc, argv, "abc:d:0123456789",
  97.                long_options, &option_index);
  98.       if (c == EOF)
  99.     break;
  100.  
  101.       switch (c)
  102.     {
  103.     case 0:
  104.       printf ("option %s", long_options[option_index].name);
  105.       if (optarg)
  106.         printf (" with arg %s", optarg);
  107.       printf ("\n");
  108.       break;
  109.  
  110.     case '0':
  111.     case '1':
  112.     case '2':
  113.     case '3':
  114.     case '4':
  115.     case '5':
  116.     case '6':
  117.     case '7':
  118.     case '8':
  119.     case '9':
  120.       if (digit_optind != 0 && digit_optind != this_option_optind)
  121.         printf ("digits occur in two different argv-elements.\n");
  122.       digit_optind = this_option_optind;
  123.       printf ("option %c\n", c);
  124.       break;
  125.  
  126.     case 'a':
  127.       printf ("option a\n");
  128.       break;
  129.  
  130.     case 'b':
  131.       printf ("option b\n");
  132.       break;
  133.  
  134.     case 'c':
  135.       printf ("option c with value `%s'\n", optarg);
  136.       break;
  137.  
  138.     case 'd':
  139.       printf ("option d with value `%s'\n", optarg);
  140.       break;
  141.  
  142.     case '?':
  143.       break;
  144.  
  145.     default:
  146.       printf ("?? getopt returned character code 0%o ??\n", c);
  147.     }
  148.     }
  149.  
  150.   if (optind < argc)
  151.     {
  152.       printf ("non-option ARGV-elements: ");
  153.       while (optind < argc)
  154.     printf ("%s ", argv[optind++]);
  155.       printf ("\n");
  156.     }
  157.  
  158.   exit (0);
  159. }
  160.  
  161. #endif /* TEST */
  162.